package com.ddgame.admob;

import org.robovm.apple.foundation.NSArray;
import org.robovm.apple.foundation.NSError;
import org.robovm.apple.gamekit.GKGameCenterControllerDelegate;
import org.robovm.apple.gamekit.GKGameCenterControllerDelegateAdapter;
import org.robovm.apple.gamekit.GKGameCenterViewController;
import org.robovm.apple.gamekit.GKGameCenterViewControllerState;
import org.robovm.apple.gamekit.GKLocalPlayer;
import org.robovm.apple.gamekit.GKScore;
import org.robovm.apple.uikit.UIViewController;
import org.robovm.apple.uikit.UIWindow;
import org.robovm.objc.block.VoidBlock1;
import org.robovm.objc.block.VoidBlock2;


/**
 * Created by chendennies on 2017/2/13.
 */

public class IOSGameCenter {

    private UIWindow mIOSWindow;
    private GameCenterListener mListener;
    public IOSGameCenter (UIWindow IOSWindow , GameCenterListener listener){
        this.mIOSWindow = IOSWindow;
        this.mListener = listener;
    }

    public void login(){
        GKLocalPlayer.getLocalPlayer().setAuthenticateHandler(new VoidBlock2<UIViewController, NSError>() {
            @Override
            public void invoke (UIViewController viewController, NSError error) {
                // If the device does not have an authenticated player, show the login dialog
                if (viewController != null) {
                    mIOSWindow.getRootViewController().presentViewController(viewController, true, null);
                }
                // If the viewController is null and the player is authenticated, the login is completed
                else if (GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
                    mListener.playerLoginCompleted();
                }
                // If the viewController is null and the player is not authenticated the login has failed
                else {
                    mListener.playerLoginFailed(error);
                }
            }
        });

    }

    public void reportScore(String identify, long score){
        System.out.println("reportScore identify=" + identify + ", score=" + score + ", isAuthenticated=" +
                GKLocalPlayer.getLocalPlayer().isAuthenticated());
        if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()){
            return;
        }

        GKScore gkScore = new GKScore();
        gkScore.setLeaderboardIdentifier(identify);
        gkScore.setValue(score);
        NSArray<GKScore> nsArray = new NSArray<>(gkScore);
        GKScore.reportScores(nsArray, new VoidBlock1<NSError>() {
            @Override
            public void invoke (NSError nsError) {
                if (nsError != null) {
                    //submitted score not successfully

                    mListener.scoreReportFailed(nsError);
                } else {
                    //submitted score successfully
                    mListener.scoreReportCompleted();
                }
            }
        });
    }


    public void showGameCenter (String identify) {
        System.out.println("isAuthenticated=" +
                GKLocalPlayer.getLocalPlayer().isAuthenticated());
        if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
            return;
        }


        GKGameCenterViewController gameCenterView = new GKGameCenterViewController();
        gameCenterView.setGameCenterDelegate(new GKGameCenterControllerDelegate() {

            @Override
            public void didFinish(GKGameCenterViewController gkGameCenterViewController) {
                gkGameCenterViewController.dismissViewController(true, null);
            }
        });
        gameCenterView.setViewState(GKGameCenterViewControllerState.Leaderboards);
        gameCenterView.setLeaderboardIdentifier(identify);
        mIOSWindow.getRootViewController().presentViewController(gameCenterView, true, null);

    }


    public void showLeaderboardsView (String identifier) {
        // If player is not authenticated, do nothing
        if (!GKLocalPlayer.getLocalPlayer().isAuthenticated()) {
            return;
        }

        // If iOS version is 6 or more we use the new method
        GKGameCenterViewController gameCenterView = new GKGameCenterViewController();
        gameCenterView.setGameCenterDelegate(new GKGameCenterControllerDelegateAdapter() {
            @Override
            public void didFinish (GKGameCenterViewController gameCenterViewController) {
                gameCenterViewController.dismissViewController(true, null);

            }
        });
        gameCenterView.setViewState(GKGameCenterViewControllerState.Leaderboards);
        gameCenterView.setLeaderboardIdentifier(identifier);

        mIOSWindow.getRootViewController().presentViewController(gameCenterView, true, null);
    }
}
